package com.wn.service;

import com.common.base.BaseService;
import com.common.entity.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wn.mapper.RoutMapper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class RoutService implements BaseService<Rout> {
    @Resource
    RoutMapper routMapper;

    @Override
    public PageInfo<Rout> searchSomeByPage(PageParam<Rout> pageParam) {
        if(!StringUtils.isEmpty(pageParam.getSuperSearchKeyWord())){
            //执行高级检索
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            //排序
            for(int i=0;i<pageParam.getOrderParams().length;i++){
                PageHelper.orderBy(pageParam.getOrderParams()[i]);
            }
            List<Rout> newscategoryList=routMapper.superSearch(pageParam.getSuperSearchKeyWord());
            PageInfo<Rout> newshotelPageInfo = new PageInfo<Rout>(newscategoryList);
            return newshotelPageInfo;
        }else{
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
            //排序
            for(int i=0;i<pageParam.getOrderParams().length;i++){
                PageHelper.orderBy(pageParam.getOrderParams()[i]);
            }
            List<Rout> programmes = routMapper.searchSomeByPage(pageParam.getModel());
            PageInfo<Rout> orderPageInfo = new PageInfo<Rout>(programmes);
            return orderPageInfo;
        }
    }

    @Override
    public Rout getOneById(String id) {
        Rout oneById = routMapper.getOneById(id);
        if (!StringUtils.isEmpty(oneById)) {
            return oneById;
        }
        return null;
    }

    @Override
    public Rsp addOne(Rout rout) {
        int i = routMapper.addOne(rout);
        if(i>0){
            return Rsp.success(rout) ;
        }
        return Rsp.fail();
    }


    @Override
    public Rsp update(Rout rout) {
        int i = routMapper.update(rout);
        if(i>0){
            return Rsp.success(routMapper.getOneById(rout.getId())) ;
        }
        return Rsp.fail();
    }

    @Override
    public Rsp deleteLogical(Rout rout) {
        rout.setDeleted(Num.DELETED_TRUE);
        int i = routMapper.update(rout);
        if(i>0){
            return Rsp.success(routMapper.getOneById(rout.getId())) ;
        }
        return Rsp.fail();
    }


    public Rsp getRoutByName(String name) {
        Rout rout=new Rout();
        rout.setName(name);
        List<Rout> i = routMapper.getRoutByName(rout);
        if(StringUtils.isEmpty(i)){
            return Rsp.fail().msg("查询失败");

        }else {
            return Rsp.success(i) ;
        }

    }

}
